package speedball;

import java.net.URL;
import java.util.ResourceBundle;

import javax.swing.JFrame;
import javax.swing.JWindow;

import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.RowConstraints;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.scene.shape.Shape;
import javafx.scene.text.Font;

public class SpeedBall {

	private int circleSize = 30;
	private int rectangleHight = circleSize - 4;
	private int rectangleWidth = rectangleHight * 7;
	private int check = 15;
	private int expandWidth = 25;

	@FXML
	private AnchorPane anchor;
	@FXML
	private Label number;

	@FXML
	private Label speed;

	@FXML
	private Label temperature;

//     @FXML
//     private Button expand;
	public void createSpeedBall() {

	}

	@FXML
	private void handleButtonAction(ActionEvent event) {
		System.out.println("You clicked me!");
		System.out.println("You clewqe!");
//        number.setText("Hello World!");
		System.out.println(number.getText());
		int ds = Integer.parseInt(number.getText());
		ds--;
		number.setText(String.valueOf(ds));

	}

	public void initialize(URL url, ResourceBundle rb) {

		createPanel();

//    	Circle circle= createCircle();

//        anchor.getChildren().add(circle);
		Button expand = createExpand(expandWidth);
		anchor.getChildren().add(expand);

		number = createNumber();
		anchor.getChildren().add(number);
		Label percent = createPercent();

		anchor.getChildren().add(percent);

		GridPane gridPane = createGridPane(false);
		anchor.getChildren().add(gridPane);
		anchor.setOpacity(0.80D);

	}

//	private Circle createCircle() {
//
//    
//    Circle circle = new Circle();
//    circle.setCenterX(circleSize);
//    circle.setCenterY(circleSize);
//    circle.setRadius(circleSize*0.98D);
//  circle.setStroke(Color.rgb(0,0,0));
//  circle.setFill(Color.rgb(0,0,0));
//		return circle;
//	}

	/**
	 * 创建面板
	 */
	private void createPanel() {
		Rectangle rect = new Rectangle(circleSize - check, circleSize - rectangleHight, rectangleWidth,
				rectangleHight * 2);
		rect.setArcHeight(rectangleHight * 2);
		rect.setArcWidth(rectangleHight * 2);

		Circle circle = new Circle();
		circle.setCenterX(circleSize);
		circle.setCenterY(circleSize);
		circle.setRadius(circleSize);
		Shape dsd = circle.union(circle, rect);
		anchor.setShape(dsd);
		anchor.setPrefWidth(circleSize - check + rectangleWidth);
		anchor.setPrefHeight(circleSize * 2);


		anchor.setOnMouseEntered(event -> {

			try {
				Thread.sleep(800L);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			if (anchor.isHover()) {

				System.out.println("67666666666666666");
			}

		});
		anchor.setOnMouseExited(event -> {
			if (!anchor.isHover()) {
				System.out.println("999999999999999999");
			}

		});
	}
	/**
	 * 创建指标分数
	 * @return
	 */
	private Label createPercent() {
		Label percent = new Label();
		percent.setText("%");
		percent.setFont(new Font("Ebrima", 12.0D));
		percent.setLayoutX(circleSize * 0.2D + number.getPrefWidth());
		percent.setLayoutY(circleSize * 0.6D + 5D);
		percent.setPrefWidth(circleSize * 2.0D * 0.4D);
		percent.setPrefHeight(circleSize * 2.0D * 0.4D);
		return percent;
	}

	/**
	 * 创建百分号
	 * @return
	 */
	private Label createNumber() {
		Label number = new Label();
		number.setText("99");
		number.setFont(new Font("Ebrima", 29.0D));
		number.setLayoutX(circleSize * 0.3D);
		number.setLayoutY(circleSize * 0.3D);
		number.setPrefWidth(circleSize * 2.0D * 0.6D);
		number.setPrefHeight(circleSize * 2.0D * 0.6D);
		number.setTextFill(Color.BROWN);
//	       number.setStyle("-fx-background-color:#434547;");
		return number;
	}

	/**
	 * 创建扩展按钮
	 * @param expandWidth 按钮宽度
	 * @return
	 */
	private Button createExpand(double expandWidth) {
		Button expand = new Button();
		expand.setPrefWidth(expandWidth);
		expand.setPrefHeight(expandWidth);
		expand.setText("+");

		expand.setOnAction(event -> {
			handleButtonAction(event);

		});
		expand.setLayoutX(circleSize - check + rectangleWidth - expandWidth - 8.0D);
		expand.setLayoutY(circleSize - expandWidth / 2);
		//"-fx-border-radius: 25; -fx-background-radius: 25; -fx-background-color: #3399FF;"
		StringBuilder style=new StringBuilder();
		style.append("-fx-border-radius: ").append(expandWidth).append("; ");
		style.append("-fx-background-radius: ").append(expandWidth).append("; ");
		style.append("-fx-background-color: ").append("#3399FF").append(";");
		
//		expand.setStyle("-fx-border-radius: 25; -fx-background-radius: 25; -fx-background-color: #3399FF;");
		expand.setStyle(style.toString());
		expand.setFont(new Font(11.0D));
		return expand;
	}

	/**
	 * 创建 参数CPU 网速 面板
	 * 
	 * @param gridLinesVisible 是否显示边界
	 * @return
	 */
	public static GridPane createGridPane(boolean gridLinesVisible) {
		double rowHeight = 20.0D;
		double colWidth = 30.0D;

		GridPane gridPane = new GridPane();
		gridPane.setLayoutX(65);
		gridPane.setLayoutY(10);
		gridPane.setPrefHeight(rowHeight * 2);
		gridPane.setPrefWidth(colWidth * 2);

		Label speed = new Label();
		speed.setText("1M/s");
		speed.setPrefHeight(rowHeight);
		speed.setPrefWidth(colWidth);
		speed.setTextFill(Color.BLACK);
		speed.setFont(new Font(9.50D));
//		speed.setFont(Font.font(null, FontWeight.THIN, 9.50D));
		
		Label CPU = new Label();
		CPU.setText("CPU");
		CPU.setTextFill(Color.rgb(102,102,102));

		Label temperature = new Label();

		temperature.setText("42℃");
		temperature.setTextFill(Color.rgb(13,215,103));// "#29ee85"

		ImageView imageView = new ImageView();

		imageView.setImage(new Image("file:C://up2.png"));
		imageView.setImage(new Image("file:C://down3.png"));
//		imageView.setImage(new Image("file:C://up.png"));

		RowConstraints rowinfo3 = new RowConstraints();
		rowinfo3.setPrefHeight(rowHeight);

		ColumnConstraints colInfo2 = new ColumnConstraints();
		colInfo2.setPrefWidth(colWidth*1.0D);

		gridPane.getRowConstraints().add(rowinfo3);// 2*50 percent
		gridPane.getRowConstraints().add(rowinfo3);
		gridPane.getColumnConstraints().add(colInfo2); // 25 percent
		gridPane.getColumnConstraints().add(colInfo2); // 50 percent
		rowinfo3.setVgrow(Priority.SOMETIMES);
		colInfo2.setHgrow(Priority.SOMETIMES); 
		
		gridPane.setConstraints(imageView, 0, 0);
		gridPane.setConstraints(speed, 1, 0);
		gridPane.setConstraints(CPU, 0, 1);
		gridPane.setConstraints(temperature, 1, 1);
		ObservableList<javafx.scene.Node> content = gridPane.getChildren();
		content.addAll(imageView, speed, CPU, temperature);
		gridPane.setGridLinesVisible(gridLinesVisible);
		return gridPane;
	}


}